// 定义默认状态
const initState = {
    display: 'all', // 显示状态(all,uncompleted,completed)
    todos: []
}
// action type
// 添加待办事项
export const ADD_TODO = 'ADD_TODO'
// 添加待办事项异步
export const ADD_TODO_ASYNC = 'ADD_TODO_ASYNC'
// 删除待办项
export const DEL_TODO = 'DEL_TODO'
// 更改待办项的完成状态
export const TOGGLE_COMPLETE = 'TOGGLE_COMPLETE'
// 更改显示待办项的状态
export const CHANGE_DISPLAY = 'CHANGE_DISPLAY'

export const Reducer = (state, action) => {
    let newState
    switch (action.type) {
        case ADD_TODO:
            newState = {
                todos: [...state.todos, action.payload]
            }
            break
        case TOGGLE_COMPLETE:
            console.log('Reducer===TOGGLE_COMPLETE==>>>')
            console.log(state)
            console.log(action.payload)
            newState = {
                todos: state.todos.map(item => {
                    if (item.id == action.payload) {
                        item.isComplete = !item.isComplete
                    }
                    return item
                })
            }
            console.log('新添加的了')
            console.log(newState)
            break
        case DEL_TODO:
            newState = {
                todos: state.todos.filter(todo => todo.id != action.payload)
            }
            break
        case CHANGE_DISPLAY:
            newState = {
                display: action.payload,
                todos: [...state.todos]
            }
            break
        default:
            newState = state
            break
    }
    return newState
}
